package com.wangjiang.mario.admin.common.base;

import com.wangjiang.mario.admin.common.rest.ResultGenerator;
import com.wangjiang.mario.admin.common.rest.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.ParameterizedType;

/**
 * 通用查询控制器
 *
 * @param <T> 实体类
 * @param <Q> 查询类，用于列表查询
 * @param <V> 实体类的返回类
 */
public class BaseSearchController<T extends BaseEntity, Q extends BaseQo, V extends BaseVo> extends BaseController<T>{

    @GetMapping("")
    public Result list (
            Q qo,
            @RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        return ResultGenerator.genSuccessResult(baseService.basePage(currentPage, pageSize, qo, getVClass()));
    }

    /**
     * 根据uuid 获取实体信息
     *
     * @param uuid
     * @return
     */
    @GetMapping("/{uuid}")
    public Result getOne (@PathVariable String uuid) {
        return ResultGenerator.genSuccessResult(baseService.baseGetByUUid(uuid, getVClass()));
    }

    private Class<V> getVClass() {
        return (Class<V>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[2];
    }
}
